package cn.gjxblog.validation.spring.demo2;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {RootConfig.class})
public class TestMethodValidationPostProcessor {

    @Autowired
    private HelloService helloService;

    /**
     * 验证方法校验是否生效
     */
    @Test
    public void test1() {
        System.out.println(helloService.getClass());
        helloService.hello(1, null);
    }

    /**
     * 验证返回值校验是否生效
     * 结论：
     *
     * 要父类（接口）和子类都要校验，先执行子类返回值上的校验注解
     * 校验通过再校验父类上的返回值校验
     */
    @Test
    public void test2() {
        System.out.println(helloService.getClass());
        helloService.hello2(12, "13");
    }

    /**
     * 验证对象校验
     *
     * savePerson对象校验不起效
     * savePerson3 对象校验生效
     *
     * 结论；
     * 校验对象需要加上@Valid
     */

    @Test
    public void test3() {
        Person person = new Person();
        person.setAge(-1);
        helloService.savePerson(person);

        helloService.savePerson3(person);
    }

    /**
     * 验证级联校验
     */
    @Test
    public void test4() {
        helloService.savePerson2(null,null);
    }

    /**
     * person 级联校验生效
     * person2 级联校验没有生效
     *
     * 结论：
     * 缺少@Valid注解
     */
    @Test
    public void test5(){
        Person person = new Person();
        person.setAge(-1);
        helloService.savePerson2(person,person);
    }
}
